clear
set more off
cd "C:\Users\emily.weber\Dropbox\intgmob_AssortativeMating\ReplicationPackage\"

cap log close

global tempdir "C:\Users\emily.weber\Dropbox\intgmob_AssortativeMating\ReplicationPackage\intermediatedata\"
global paneltemp "C:\Users\emily.weber\Dropbox\intgmob_AssortativeMating\ReplicationPackage\intermediatedata\panel\"
global ipumsdir "C:\Users\emily.weber\Dropbox\intgmob_AssortativeMating\ReplicationPackage\rawdata\census_1pct\"
global histdir "C:\Users\emily.weber\Dropbox\intgmob_AssortativeMating\ReplicationPackage\rawdata\icpsr\"


***Create locals with mean size for consistent buckets

local c_09 = 4.5
local c_1019 = 14.5
local c_2049 = 34.5
local c_5099 = 74.5
local c_100 = 299.5
local c_500 = 749.5
local c_1000 = 1000

*************************************************
*1890
*************************************************

*Locals

local d_09 = 4.5
local d_1019 = 14.5
local d_2049 = 34.5
local d_5099 = 74.5
local d_100 = 299.5
local d_500 = 749.5
local d_1000 = 1000

use $histdir/ICPSR_1890_stct.dta, clear

gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)
gen ginifarmsize_con = ginifarmsize_diff

drop if fips == .
keep if level == 2
keep year fips farms ginifarmsize_diff ginifarmsize_con farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000
replace fips = fips/1000
sleep 1000
save $paneltemp/ginifarmsize.dta, replace

*************************************************
*1860
*************************************************

*Locals

local d_02 = 1
local d_39 = 6

use $histdir/ICPSR_1860_stct.dta, clear
gen year = 1860
keep if level == 2
keep year fips name farm*

replace fips = fips/1000
merge 1:1 fips using $histdir/ICPSR_1860_st.dta, keepusing(farms) nogen
gen farm02 = farms - (farm39 + farm1019 + farm2049 + farm5099 + farm100 + farm500 + farm1000)

gen numer = 0
gen denom = 0
gen total = 0

foreach i in 02 39 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`d_`i'' * coeff_`i')
replace denom = (farm`i' * `d_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)

drop numer denom total start_* coeff_* end_*

gen farm09 = farm02 + farm39
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_con = 1 + (1/farms) - 2*(numer/denom)

drop if fips == .
keep year fips farms ginifarmsize_diff ginifarmsize_con farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000
sleep 1000
append using $paneltemp/ginifarmsize.dta
save $paneltemp/ginifarmsize.dta, replace


*************************************************
*1870
*************************************************

*Locals

local d_02 = 1
local d_39 = 6

use $histdir/ICPSR_1870_stct.dta, clear
gen year = 1870
keep if level == 2
keep year fips name farm*

gen numer = 0
gen denom = 0
gen total = 0

foreach i in 02 39 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`d_`i'' * coeff_`i')
replace denom = (farm`i' * `d_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)

drop numer denom total start_* coeff_* end_*

gen farm09 = farm02 + farm39
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_con = 1 + (1/farms) - 2*(numer/denom)

drop if fips == .
keep year fips farms ginifarmsize_diff ginifarmsize_con farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000
replace fips = fips/1000
sleep 1000
append using $paneltemp/ginifarmsize.dta
save $paneltemp/ginifarmsize.dta, replace

*************************************************
*1880
*************************************************

use $histdir/ICPSR_1880_stct.dta, clear

gen numer = 0
gen denom = 0
gen total = 0

foreach i in 02 39 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`d_`i'' * coeff_`i')
replace denom = (farm`i' * `d_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)

drop numer denom total start_* coeff_* end_*

gen farm09 = farm02 + farm39
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_con = 1 + (1/farms) - 2*(numer/denom)

drop if fips == .
keep if level == 2
keep year fips farms ginifarmsize_diff ginifarmsize_con farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000
replace fips = fips/1000
sleep 1000
append using $paneltemp/ginifarmsize.dta
save $paneltemp/ginifarmsize.dta, replace

*************************************************
*1900
*************************************************

*Locals

local d_02 = 1
local d_12 = 1.5
local d_39 = 6
local d_1019 = 14.5
local d_2049 = 34.5
local d_5099 = 74.5
local d_100 = 137
local d_175 = 217
local d_260 = 379.5
local d_500 = 749.5
local d_1000 = 1000

use $histdir/ICPSR_1900_stct.dta, clear

gen numer = 0
gen denom = 0
gen total = 0

foreach i in 12 39 1019 2049 5099 100 175 260 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`d_`i'' * coeff_`i')
replace denom = (farm`i' * `d_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)

drop numer denom total start_* coeff_* end_*

gen c_farm09 = farm12 + farm39
gen c_farm1019 = farm1019
gen c_farm2049 = farm2049
gen c_farm5099 = farm5099
gen c_farm100 = farm100 + farm175 + farm260
gen c_farm500 = farm500
gen c_farm1000 = farm1000
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if c_farm`i' > 0
gen end_`i' = (total + c_farm`i') if c_farm`i' > 0
replace end_`i' = total if c_farm`i' == 0
gen coeff_`i' = c_farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if c_farm`i' > 0
replace coeff_`i' = 0 if c_farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (c_farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_con = 1 + (1/farms) - 2*(numer/denom)

drop if fips == .
keep if level == 2
keep year fips farms ginifarmsize_diff ginifarmsize_con c_*

foreach var in  farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000 {
	rename c_`var' `var'
	}
	
replace fips = fips/1000
sleep 1000
append using $paneltemp/ginifarmsize.dta
save $paneltemp/ginifarmsize.dta, replace

*************************************************
*1910 and 1920
*************************************************

foreach y in 1910 1920{
use $histdir/ICPSR_`y'_stct.dta, clear
gen year = `y'
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 02 39 1019 2049 5099 100 175 260 500 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`d_`i'' * coeff_`i')
replace denom = (farm`i' * `d_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)

drop numer denom total start_* coeff_* end_*

gen c_farm09 = farm02 + farm39
gen c_farm1019 = farm1019
gen c_farm2049 = farm2049
gen c_farm5099 = farm5099
gen c_farm100 = farm100 + farm175 + farm260
gen c_farm500 = farm500
gen c_farm1000 = farm1000
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if c_farm`i' > 0
gen end_`i' = (total + c_farm`i') if c_farm`i' > 0
replace end_`i' = total if c_farm`i' == 0
gen coeff_`i' = c_farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if c_farm`i' > 0
replace coeff_`i' = 0 if c_farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (c_farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_con = 1 + (1/farms) - 2*(numer/denom)

drop if fips == .
keep if level == 2
keep year fips farms ginifarmsize_diff ginifarmsize_con c_*

foreach var in  farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000 {
	rename c_`var' `var'
	}
	
replace fips = fips/1000
sleep 1000
append using $paneltemp/ginifarmsize.dta
save $paneltemp/ginifarmsize.dta, replace
}

*************************************************
*1930
*************************************************

*Locals

local d_1000 = 2999.5
local d_5000 = 5000

use $histdir/ICPSR_1930_stct.dta, clear
gen year = 1930
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 02 39 1019 2049 5099 100 175 260 500 1000 5000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`d_`i'' * coeff_`i')
replace denom = (farm`i' * `d_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)

drop numer denom total start_* coeff_* end_*

gen c_farm09 = farm02 + farm39
gen c_farm1019 = farm1019
gen c_farm2049 = farm2049
gen c_farm5099 = farm5099
gen c_farm100 = farm100 + farm175 + farm260
gen c_farm500 = farm500
gen c_farm1000 = farm1000 + farm5000
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if c_farm`i' > 0
gen end_`i' = (total + c_farm`i') if c_farm`i' > 0
replace end_`i' = total if c_farm`i' == 0
gen coeff_`i' = c_farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if c_farm`i' > 0
replace coeff_`i' = 0 if c_farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (c_farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_con = 1 + (1/farms) - 2*(numer/denom)

drop if fips == .
keep if level == 2
keep year fips farms ginifarmsize_diff ginifarmsize_con c_*

foreach var in  farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000 {
	rename c_`var' `var'
	}
	
replace fips = fips/1000
sleep 1000
append using $paneltemp/ginifarmsize.dta
save $paneltemp/ginifarmsize.dta, replace

*************************************************
*1940
*************************************************

*Locals

local d_02 = 1
local d_39 = 6
local d_1029 = 19.5
local d_3049 = 39.5
local d_5069 = 59.5
local d_7099 = 84.5
local d_100 = 119.5
local d_140 = 159.5
local d_180 = 199.5
local d_220 = 239.5
local d_260 = 319.5
local d_380 = 439.5
local d_500 = 599.5
local d_700 = 849.5
local d_1000 = 1000

use $histdir/ICPSR_1940_stct.dta, clear
gen year = 1940
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 02 39 1029 3049 5069 7099 100 140 180 220 260 380 500 700 1000{
gen start_`i' = (total + 1) if farm`i' > 0
gen end_`i' = (total + farm`i') if farm`i' > 0
replace end_`i' = total if farm`i' == 0
gen coeff_`i' = farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if farm`i' > 0
replace coeff_`i' = 0 if farm`i'==0
replace total = end_`i'

replace numer = numer + (`d_`i'' * coeff_`i')
replace denom = (farm`i' * `d_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_diff = 1 + (1/farms) - 2*(numer/denom)

drop numer denom total start_* coeff_* end_*

gen c_farm09 = farm02 + farm39
gen c_farm1019 = farm1019
gen c_farm2049 = farm1029 + farm3049 - farm1019
gen c_farm5099 = farm5069 + farm7099
gen c_farm100 = farm100 + farm140 + farm180 + farm220 + farm260 + farm380
gen c_farm500 = farm500 + farm700
gen c_farm1000 = farm1000
gen numer = 0
gen denom = 0
gen total = 0

foreach i in 09 1019 2049 5099 100 500 1000{
gen start_`i' = (total + 1) if c_farm`i' > 0
gen end_`i' = (total + c_farm`i') if c_farm`i' > 0
replace end_`i' = total if c_farm`i' == 0
gen coeff_`i' = c_farm`i' * (farms + 1 - (start_`i'+ end_`i')/2) if c_farm`i' > 0
replace coeff_`i' = 0 if c_farm`i'==0
replace total = end_`i'

replace numer = numer + (`c_`i'' * coeff_`i')
replace denom = (c_farm`i' * `c_`i'') + denom
}

replace denom = farms * denom
gen ginifarmsize_con = 1 + (1/farms) - 2*(numer/denom)

drop if fips == .
keep if level == 2
keep year fips farms ginifarmsize_diff ginifarmsize_con c_*

foreach var in  farm09 farm1019 farm2049 farm5099 farm100 farm500 farm1000 {
	rename c_`var' `var'
	}
	
replace fips = fips/1000
sleep 1000
append using $paneltemp/ginifarmsize.dta
save $paneltemp/ginifarmsize.dta, replace

